﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.EnhancedEntities;
using MovieRental_Presentation.Helpers;
using System.Collections.ObjectModel;

namespace MovieRental_Presentation.DataViews
{
    public class CustomerDetailsDataView : BaseDataView
    {
        private Customer _customer;
        private bool _isReadOnly;
        private ObservableCollection<LoanCart> _carts = new ObservableCollection<LoanCart>();

        public CustomerDetailsDataView(Guid customerId)
        {
            _customer = new Customer(ComponentManager.Instance.CustomerComponent.Get(customerId));
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("Carts");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("Customer");
        }

        public Customer Customer
        {
            set { SetProperty<Customer>(ref _customer, value, "Customer"); }
            get { return _customer; }     
        }

        public bool IsReadOnly
        {
            set { SetProperty<bool>(ref _isReadOnly, value, "IsReadOnly"); }
            get { return _isReadOnly; }
        }

        public ObservableCollection<LoanCart> Carts
        {
            get 
            { 
                var query = ComponentManager.Instance.LoanCartComponent.GetAllNotReturnedForCustomer(Customer.Entity).Select(x => new LoanCart(x));
                UpdateObservableCollection<LoanCart>(_carts, query);
                return _carts;
            }
        }

    }
}
